package com.rainner.admin.server;

import java.sql.SQLException;
import java.util.List;

import com.rainner.admin.javabean.BookBean;
import com.rainner.admin.util.dbutil.BaseService;

public class BookServer extends BaseService<BookBean> {
	/**
	 * 返回一页的书籍信息
	 * 
	 * @param rows
	 * @param page
	 * @return
	 * @throws SQLException
	 */
	public List<BookBean> allBookData(long rows, long page) throws SQLException {
		String sqlSelect = "select * from Book limit ?,?";
		return super.query(sqlSelect, (page - 1) * rows, rows);
	}

	/**
	 * 返回书籍总数量
	 * 
	 * @return
	 * @throws SQLException
	 */
	public long countBookData() throws SQLException {
		String sqlSelect = "select COUNT(1) from Book";
		return super.counts(sqlSelect);
	}

	/**
	 * 新增书籍信息
	 * 
	 * @param book
	 * @return
	 * @throws SQLException
	 */
	public boolean addBookData(BookBean book) throws SQLException {
		String sqlAdd = "insert into Book(BookId,ManagerName,BookName,BookAuthor,BookNumber,BookPublish,BookSummary,BookType,BookImg,BookDate) values(?,?,?,?,?,?,?,?,?,?)";
		try {
			return super.update(sqlAdd, book.getBookId(), book.getManagerName(), book.getBookName(), book.getBookAuthor(), book.getBookNumber(), book.getBookPublish(), book.getBookSummary(), book.getBookType(), book.getBookImg(), book.getBookDate());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 删除书籍
	 * 
	 * @param book
	 * @return
	 * @throws SQLException
	 */
	public boolean delBookData(BookBean book) throws SQLException {
		String sqlDel = "delete from Book where BookId = ?";
		return super.update(sqlDel, book.getBookId());
	}

	/**
	 * 修改书籍信息
	 * 
	 * @param book
	 * @return
	 * @throws SQLException
	 */
	public boolean updateBookData(BookBean book) throws SQLException {
		String sqlUpdate = "update Book set ManagerName = ?,BookName = ?,BookAuthor = ?,BookNumber = ?,BookPublish = ?,BookSummary = ?,BookType = ?,BookImg = ?,BookDate = ? where BookId = ?";
		return super.update(sqlUpdate, book.getManagerName(), book.getBookName(), book.getBookAuthor(), book.getBookNumber(), book.getBookPublish(), book.getBookSummary(), book.getBookType(), book.getBookImg(), book.getBookDate(), book.getBookId());
	}

	/**
	 * 返回书籍信息
	 * 
	 * @param name
	 * @param value
	 * @param begin
	 * @return
	 * @throws SQLException
	 */
	public List<BookBean> allBookData(String name, String value, long rows, long page) throws SQLException {
		String sqlSelect;
		value = "%" + value + "%";
		sqlSelect = "select * from Book where " + name + " like ? limit ?,?";
		return super.query(sqlSelect, value, (page - 1) * rows, rows);

	}

	/**
	 * 返回书籍总数
	 * 
	 * @return
	 * @throws SQLException
	 */
	public long countBookData(String name, String value) throws SQLException {
		String sqlSelect;
		value = "%" + value + "%";
		sqlSelect = "select COUNT(1) from Book where " + name + " like ? ";
		return super.counts(sqlSelect, value);
	}
}
